home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2003 March / DPPCPRO0303.ISO / Components / Microsoft ASP / _SETUP.1 / MSDBConnection.inc < prev    next >
Encoding:
Text File  |  1998-11-20  |  8.0 KB  |  216 lines

  1. </SCRIPT><SCRIPT RUNAT=SERVER LANGUAGE=JSCRIPT>
  2. <!--#INCLUDE FILE="MSGlobal.inc"-->
  3. ////////////////////////////////////////////////////////////////////
  4. //                                                                //
  5. //   NetObjects Fusion ASP Components                             //
  6. //   Custom JScript Object: MSDBConnection                       // 
  7. //                                                                //
  8. ////////////////////////////////////////////////////////////////////
  9. /*
  10. Object:       MSDBConnection 
  11.  
  12. Version:      1.0  9/23/97
  13.  
  14. Written By:   Application Methods, Inc.
  15.               6300 Southcenter Blvd.
  16.               Seattle, WA 98188
  17.               (206) 244-2400
  18.               http://www.appmethods.com
  19.  
  20. DESCRIPTION: This object definition describes properties and methods for a dynamically created
  21. ASP database connection. This function allows a developer to pass a set of database
  22. parameters to the object and connect to a database. The parameters are defined as follows
  23.  
  24.    
  25. Properties:
  26.     name             - The name of the MSDBConnection object
  27.     databaseType     - Type of connection, e.g. ODBC, native driver, etc.  All connections are ODBC for these components.
  28.     ODBCDatabaseType - One of the supported ODBC database types, such as MS Access, MS SQL Server, etc.
  29.     databaseServer   - The name of your data source pointing to the appropriate database.
  30.     username         - The name of the user allowed DB access
  31.     password         - associated pass word for the above user
  32.     globalConn       - a string representation of a boolean value indicating whether or not the global.asa file will
  33.                        be used to establish the database connection (if true) or one DBConnection component
  34.                        per page will be used (false).
  35.  
  36. Methods:
  37.    connect             - Connects to database
  38.    disconnect         - Disconnects from database
  39.    connected         - Returns whether the database is currently connected
  40.    render             - renders hidden visual elements of database component 
  41.    emitProperties     - emits database component's properties
  42.  
  43. NOTE: All of the above parameters may not be meaningful based on the type of connection
  44. that is selected. Please consult the ASP manual and the database vendor for details.
  45.  
  46. Note: Generation of a number of hidden fields is necessary to pass information
  47.    on to other components.  All hidden fields created by this object are prefixed
  48.    with 'amaspHidden_' or 'amaspComponent_'.  Developers should never create
  49.    any form element with these prefixes or unexpected results will occur.
  50.  
  51.  
  52. USAGE:
  53. <HTML>
  54. <HEAD>
  55.  
  56. <!SCRIPT RUNAT=SERVER LANGUAGE=JSCRIPT>
  57.  
  58.     test = new MSDBConnection("test", "ODBC","MS Access", "Northwind","admin","",false);
  59.     if (test.connect()) Response.write("The Database connected properly<BR>")
  60.         else Response.write ("The database did not connect<BR>");
  61.    
  62.     if (database.connect()) write ("The Database is connected<BR>")
  63.         else Response.write ("The Database is not connected<BR>");
  64.    
  65.     write ("The Database is being disconnected<BR>");   
  66.     dbResult = test.emitProperties();
  67.     test.render();
  68.  
  69.     dbResult = test.disconnect();
  70.     Response.write ("The database is now disconnected: <B>" + dbResult + "</B><BR>");
  71.  
  72. <!/SCRIPT>
  73.  
  74. </HEAD>
  75. <BODY>
  76. <%
  77.    // This is only necessary if the component will be used with other components
  78.    MSDBConnection1.render()  
  79. %>
  80.  
  81. </BODY>
  82. </HTML>
  83.  
  84. =====================================================================*/
  85. function MSDBConnection(name,
  86.                         databaseType,
  87.                         ODBCDatabaseType,
  88.                         databaseServer,
  89.                         userName,
  90.                         password,
  91.                         globalConn) 
  92.     // Properties
  93.    this.databaseType      = "ODBC";    // All ASP connections are via ODBC
  94.    this.ODBCDatabaseType  = ODBCDatabaseType;
  95.    this.databaseServer    = databaseServer;
  96.    this.userName          = userName;
  97.    this.password          = password;
  98.    this.globalConn        = globalConn;
  99.    this.connection          = null;
  100.    this.name              = name;    
  101.    Session(this.name)      = null;
  102.     
  103.    // Methods
  104.    this.connect = dbConnect;
  105.    this.disconnect = dbDisconnect;
  106.    this.connected = dbConnected;
  107.    this.render = dbRender;
  108.    this.emitProperties = dbEmitProperties;
  109.    
  110.    // Global connect commands removed because done in Global.asa file
  111.    // Set Application("globalConn") here too
  112.    Application("globalConn") = globalConn;
  113.  
  114.    // Write out to session object for use by Query component 
  115.    Session("databaseType") = this.databaseType;
  116.    Session("ODBCDatabaseType") = this.ODBCDatabaseType;
  117.  
  118. } //End MSDBConnection 
  119.  
  120.  
  121.  
  122. // Method - dbConnect()
  123. // Attempts to make a connection to the database using the Database Object, if the database is not already
  124. // connected. If the database is already connected, the method returns true.
  125.  
  126. function dbConnect ()
  127. {
  128.    if (!this.connected())
  129.    {
  130.         var connection = new Object();
  131.         connection = Server.CreateObject("ADODB.Connection");
  132.         connection.Open(this.databaseServer, this.userName, this.password);
  133.  
  134.        //if (ConnectionErrors(connection) == 1) {
  135.            // Store connection object in Session object for reference by DBQuery component
  136.            Session(this.name) = null;
  137.            Session(this.name) = connection;
  138.             return true;
  139.       //}
  140.       //else
  141.             //return false;
  142.     } // end if (!this.connected())
  143.     
  144.    else return true;   
  145. }   //End dbConnect;
  146.  
  147.  
  148. // Method - dbDisconnect
  149. // Attempts to disconnect the Database if it's connected. Otherwise returns a true result if the database is already
  150. // disconnected.
  151.  
  152. function dbDisconnect ()
  153. {
  154.    if (this.connected())
  155.    Session(this.name).Close();
  156.    return true;
  157. }   //End dbDisconnect
  158.  
  159. // Method - dbConnected
  160. // Attempts to determine the state of the connection. If the connection exists then this function returns true.
  161. // Otherwise, false is returned.
  162.  
  163. function dbConnected ()
  164. {
  165.     if (Session(this.name) != null)
  166.         //{
  167.         // ConnectionErrors() is a VBScript function that checks the connection object's errors collection.
  168.         // This is a workaround for not being able to reference ADO connection objects' errors collections
  169.         // from within JScript functions.
  170.         //if (ConnectionErrors(Session(this.name)) == 1)
  171.             return true;
  172.         //else
  173.             //return false;
  174.         //}
  175.     else
  176.         return false;
  177. }   //End dbConnected
  178.  
  179. // Method - dbEmitProperties()
  180. // emits all the object's properties.  Used as a debuging tool to verify correct
  181. // manipulation of the object.
  182.  
  183. function dbEmitProperties ()
  184. {
  185.       //debug("Writing MSDBConnection properties...");
  186.       write("\n<BR><B>ASPDBConnection Properties:</B>");
  187.       write("\n<BR>databaseType = " + this.databaseType);
  188.       write("\n<BR>ODBCDatabaseType = " + this.ODBCDatabaseType);
  189.       write("\n<BR>databaseServer = " + this.databaseServer);
  190.       write("\n<BR>userName = " + this.userName);
  191.       write("\n<BR>password = " + this.password);
  192.       write("\n<BR>globalConn = " + this.globalConn);
  193.       write("<BR>");
  194.       return true;
  195.  }   //End dbEmitProperties;
  196.  
  197. // Method - dbRender
  198. // This method outputs the hidden fields that contain the values of the database
  199. // connection parameters. This is used by the form handler that corresponds to the
  200. // UpdateTable component.
  201.  
  202. function dbRender ()
  203. {
  204.   // Output the hidden fields for use by Update component
  205.    var pre = "amaspHidden_" + this.name + "_";
  206.    write("\r\n<INPUT NAME = \"" + pre + "ODBCDatabaseType\" VALUE = \"" + this.ODBCDatabaseType + "\" TYPE = \"HIDDEN\">");
  207.    write("\r\n<INPUT NAME = \"" + pre + "databaseType\" VALUE = \"" + this.databaseType + "\" TYPE = \"HIDDEN\">");
  208.    write("\r\n<INPUT NAME = \"" + pre + "databaseServer\" VALUE = \"" + this.databaseServer + "\" TYPE = \"HIDDEN\">");
  209.    write("\r\n<INPUT NAME = \"" + pre + "userName\" VALUE = \"" + this.userName + "\" TYPE = \"HIDDEN\">");
  210.    write("\r\n<INPUT NAME = \"" + pre + "password\" VALUE = \"" + this.password + "\" TYPE = \"HIDDEN\">");
  211.    write("\r\n<INPUT NAME = \"" + pre + "globalConn\" VALUE = \"" + this.globalConn + "\" TYPE = \"HIDDEN\">");
  212.   
  213.  
  214. }   //End dbRender
  215. </SCRIPT>